<?php

namespace app\index\controller;

// use app\common\controller\Frontend;
use app\index\controller\User;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Session;

/**
 * 流量情况
 */
class Screen extends User


{    
    protected $layout = 'defaults';

    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");       
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }
    

    public function index()
    {
        $this->view->engine->layout('layout/' . $this->layout);
        return view();
    }



    public function app_quality()
    {
        $data = ['video','basedlink','webpage','filedownload'];
        $count = count($data);
        $row = [];
        for ($i=0; $i < $count; $i++) { 
            $row[$i] = $this->get_app($data[$i]);
        }
        $row = json_encode($row);
        return $row;
    }


    public function get_app ($name)
    {
        $data = array(
            'token' => session('login_token'),
            "propertyname" => $name,
            'terminal' => 'web',
        );
        $data_string = json_encode($data);
        $url = request()->domain() . '/api/nqm/type_quality';
        ##  调内部接口
        $res = $this->curl($url,$data_string);        
        $res = json_decode($res,true);
        return $res;
    }






    /**
     * 透传内部接口
     *
     * @param      <type>  $url    The url
     * @param      <type>  $data   The data
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function curl($url,$data_string){                
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data_string ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );        
        //发请求拿数据
        $data = curl_post($url, $data_string, $curlopt);        
        return $data;
    } 
}
